#include "system.h"
#include "main.h"
#include <string.h>

uint8_t backLightEn = 1;
uint8_t keyxUpLoadEn[KEY_NUM] = {0};
uint8_t unRegisterHint;
uint8_t unRegisterHintStep;
uint8_t isInfoSynchronize;

void system_init(void)
{
    uint8_t relayState[KEY_NUM];
    uint8_t isInited;
    eeprom_read_bytes(EEPROM_ADDR_INITED, &isInited, 1);
    if(isInited == 'M')
    {
        eeprom_read_bytes(EEPROM_ADDR_BACKLIGHT_EN, &backLightEn, 1);
        eeprom_read_bytes(EEPROM_ADDR_KEY1_UPLOAD_EN, keyxUpLoadEn, KEY_NUM);
        eeprom_read_bytes(EEPROM_ADDR_RELAY1, relayState, KEY_NUM);
        for(uint8_t i = 0; i < KEY_NUM; i++)
        {
            relay_set(i, relayState[i]);
            led_set(i, relayState[i]);
            if(backLightEn)
            {
                led_set(i + 8, !relayState[i]);
            }
        }
    }
    else
    {
        system_factory();
    }
    
    debug("BackLight_En = %d\r\n", backLightEn);
    for(uint8_t i = 0; i < KEY_NUM; i++)
    {
        debug("Key%dUpLoad_En = %d\r\n", i + 1, keyxUpLoadEn[i]);
    }
    for(uint8_t i = 0; i < KEY_NUM; i++)
    {
        debug("Relay%dState = %d\r\n", i + 1, get_relay_state(i));
    }
}

void system_process(void)
{
    BtnState state;
    static uint8_t delay200msCnt;
    static uint8_t factoryStep;
    for(uint8_t i = 0; i < KEY_NUM; i++)
    {
        state = button_get_state(i);
        if(state == Pressed)
        {
            led_toogle(i);
            if(keyxUpLoadEn[i] == 0)
            {
                relay_toogle(i);
            }
            comm_send_key_state(i, get_led_state(i));
            if(factoryStep != 0)
            {
                factoryStep = 0;
            }
        }
        if(i == 0)
        {
            if(state == LongPressed)
            {
                switch(factoryStep)
                {
                    case 0:
                    {
                        factoryStep = 1;
                        delay200msCnt = 0;
                    }
                    break;
                    case 1:
                    {
                        if(delay200msCnt >= 15 && delay200msCnt <= 45)
                        {
                            factoryStep = 0;
                            
                            /* 此处调用恢复出厂设置函数 */
                            system_factory();
                            comm_send_factory();
                        }
                    }
                    break;
                }
            }
            if(factoryStep == 1)
            {
                delay200msCnt++;
                if(delay200msCnt >= 45)
                {
                    delay200msCnt = 0;
                    factoryStep = 0;
                }
                else if((delay200msCnt >= 1 && delay200msCnt <= 4) || (delay200msCnt >= 15 && delay200msCnt <= 18))
                {
                    for(uint8_t i = 0; i < KEY_NUM; i++)
                    {
                        led_toogle(i);
                    }
                }
            }
        }
        if(unRegisterHint == 0)
        {
            if(backLightEn)
            {
                if(get_led_state(i))
                {
                    led_set(i + 8, 0);
                }
                else
                {
                    led_set(i + 8, 1);
                }
            }
            else
            {
                led_set(i + 8, 0);
            }
        }
    }
    if(unRegisterHint)
    {
        static uint32_t counter;
        static uint8_t backLightState;
        if(counter > (6000))
        {
            counter = 0;
            unRegisterHint = 0;
        }
        else if(++counter % 5 == 0)
        {
            backLightState = !backLightState;
            for(uint8_t i = 0; i < KEY_NUM; i++)
            {
                if(get_led_state(i) == 0)
                {
                    led_set(i + 8, backLightState);
                }
                else
                {
                    led_set(i + 8, 0);
                }
            }
        }
    }
    else
    {
        if(isInfoSynchronize == 0)
        {
            isInfoSynchronize = 1;
            for(uint8_t i = 0; i < KEY_NUM; i++)
            {
                comm_send_key_state(i, get_led_state(i));
            }
        }
    }
}

void system_factory(void)
{
    /* 恢复出厂设置成功，闪烁5s */
    for(uint8_t i = 0; i < 5; i++)
    {
        for(uint8_t j = 0; j < KEY_NUM; j++)
        {
            led_set(j, 0);
            led_set(j + 8, 0);
        }
        delay_ms(500);
        for(uint8_t j = 0; j < KEY_NUM; j++)
        {
            led_set(j, 1);
            led_set(j + 8, 1);
        }
        delay_ms(500);
    }

    backLightEn = 1;
    eeprom_write_one_byte(EEPROM_ADDR_BACKLIGHT_EN, backLightEn);

    for(uint8_t i = 0; i < KEY_NUM; i++)
    {
        keyxUpLoadEn[i] = 0;
        eeprom_write_one_byte(EEPROM_ADDR_KEY1_UPLOAD_EN + i, keyxUpLoadEn[i]);
    }
    
    for(uint8_t i = 0; i < KEY_NUM; i++)
    {
        relay_set(i, 0);
        led_set(i, 0);
        led_set(i + 8, 1);
        eeprom_write_one_byte(EEPROM_ADDR_RELAY1 + i, 0);
    }
    eeprom_write_one_byte(EEPROM_ADDR_INITED, 'M');
}
